﻿using GalaSoft.MvvmLight;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using System.Collections;
using System;

namespace DiagramDesigner
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ContoursViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the ContoursViewModel class.
        /// </summary>
        public ContoursViewModel()
        {            
            PointCollection pc = new PointCollection();
            pc.Add(new Point(10, 20));
            pc.Add(new Point(40, 20));
            pc.Add(new Point(70, 30)); 
            
            Contours.Add(pc);

            pc = new PointCollection();
            pc.Add(new Point(20, 50));
            pc.Add(new Point(40, 60));
            pc.Add(new Point(60, 90));
            
            Contours.Add(pc);


            AngleLineLeft = new LineModel()
            {
                Pt1 = new Point(50.5,200.5),
                Pt2 = new Point(100.5,20.5)
            };
            
            Substrate = new LineModel()
            {
                Pt1 = new Point(100.5, 100.5),
                Pt2 = new Point(400.5, 100.5)
            };           
        }


        #region Properties
        /// <summary>
        /// The <see cref="SelectedCount" /> property's name.
        /// </summary>
        public const string SelectedCountPropertyName = "SelectedCount";

        private bool _myProperty = false;

        /// <summary>
        /// Sets and gets the SelectedCount property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool SelectedCount
        {
            get
            {
                return _myProperty;
            }

            set
            {
                if (_myProperty == value)
                {
                    return;
                }

                _myProperty = value;
                RaisePropertyChanged(SelectedCountPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="Contours" /> property's name.
        /// </summary>
        public const string ContoursPropertyName = "Contours";

        private ObservableCollection<PointCollection> _contours = new ObservableCollection<PointCollection>();

        /// <summary>
        /// Sets and gets the Contours property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<PointCollection> Contours
        {
            get
            {
                return _contours;
            }

            set
            {
                if (_contours == value)
                {
                    return;
                }

                _contours = value;
                RaisePropertyChanged(ContoursPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="Substrate" /> property's name.
        /// </summary>
        public const string SubstratePropertyName = "Substrate";

        private LineModel _substrate = null;

        /// <summary>
        /// Sets and gets the Substrate property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public LineModel Substrate
        {
            get
            {
                return _substrate;
            }

            set
            {
                if (_substrate == value)
                {
                    return;
                }

                _substrate = value;
                RaisePropertyChanged(SubstratePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="AngleLineLeft" /> property's name.
        /// </summary>
        public const string AngleLineLeftPropertyName = "AngleLineLeft";

        private LineModel _angleLineLeft = null;

        /// <summary>
        /// Sets and gets the AngleLineLeft property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public LineModel AngleLineLeft
        {
            get
            {
                return _angleLineLeft;
            }

            set
            {
                if (_angleLineLeft == value)
                {
                    return;
                }

                _angleLineLeft = value;
                RaisePropertyChanged(AngleLineLeftPropertyName);
            }
        }

        #endregion 


        private ArrayList selectedItems;

        #region Commands
        private RelayCommand<IList> _selectionChangedCommand;

        /// <summary>
        /// Gets the SelectionChanged.        
        /// </summary>
        public RelayCommand<IList> SelectionChangedCommand
        {
            get
            {
                return _selectionChangedCommand
                    ?? (_selectionChangedCommand = new RelayCommand<IList>(
                                          items =>
                                          {
                                              selectedItems = new ArrayList(items);
                                          }));
            }
        }

        private RelayCommand _deleteCommand;

        /// <summary>
        /// Gets the DeleteCommand.
        /// </summary>
        public RelayCommand DeleteCommand
        {
            get
            {
                return _deleteCommand
                    ?? (_deleteCommand = new RelayCommand(
                                          () =>
                                          {
                                              if (selectedItems == null)
                                                  return;

                                              foreach (object item in selectedItems)
                                              {
                                                  if (item is PointCollection)
                                                  {
                                                      _contours.Remove((PointCollection)item);
                                                  }
                                              }
                                          }));
            }
        }

        private RelayCommand _addCommand;

        /// <summary>
        /// Gets the AddCommand.
        /// </summary>
        public RelayCommand AddCommand
        {
            get
            {
                return _addCommand ?? (
                    _addCommand = new RelayCommand(
                        () =>
                        {
                            PointCollection pc = new PointCollection();
                            Random r = new Random();
                            for (int i = 0; i < 10; ++i)
                            {
                                pc.Add(new Point(i * 10, 150 + r.NextDouble() * 100));
                            }
                            _contours.Add(pc);
                        }));
            }
        }

        #endregion
    }

}